<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>智能牧犬监控 | 登录</title>
    <!-- 告诉浏览器该页面是自适应布局 -->
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <!-- Bootstrap 3.3.7 -->
    <link rel="stylesheet" href="../bootstrap/css/bootstrap.min.css">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="../dist/css/font-awesome.css">
    <!-- Ionicons -->
    <link rel="stylesheet" href="../dist/css/ionicons.css">
    <!-- Theme style -->
    <link rel="stylesheet" href="../dist/css/AdminLTE.min.css">
    <!-- iCheck -->
    <link rel="stylesheet" href="../dist/css/blue.css">

    <link rel="shortcut icon" href="../dist/img/favicon.ico" />
    <link rel="bookmark" href="../dist/img/favicon.ico" type="image/x-icon"　/>

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- 警告：Respond.js 不支持 file:// 方式查看（即本地方式查看）-->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->

    <!-- this page specific styles -->
    <link rel="stylesheet" href="ui_css/login.css" type="text/css" media="screen" />
    <!-- Google Font -->
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700,300italic,400italic,600italic">
</head>
<body class="hold-transition login-page"  onkeydown="keyLogin();">
    <div class="login-box-bg"></div>
<div class="login-box" >
    <div class="login-logo">
        <a href="#"><b>包虫病防控犬驱虫<br/>远程管理系统</br></a>
    </div>
    <!-- /.login-logo -->
    <div class="login-box-body">
        <p class="login-box-msg">帐户登录</p>

        <form>
            <div class="form-group has-feedback">
                <input type="username" id="username" class="form-control" placeholder="用户名">
                <span class="glyphicon glyphicon-user form-control-feedback"></span>
            </div>
            <div class="form-group has-feedback">
                <input type="password" id="password" class="form-control" placeholder="密码">
                <span class="glyphicon glyphicon-lock form-control-feedback"></span>
            </div>
            <div class="row">
                <div class="col-xs-8">
                    <div class="checkbox icheck">
                        <label>
                            <input id="remember_me"  type="checkbox"><span class="checkbox-span">记住我</span>
                        </label>
                    </div>
                </div>
                <!-- /.col -->
                <div class="col-xs-4">
                    <div id="forgetpwd" class="login-forget text-right"><a href="#">忘记密码？</a></div>
                </div>
                <!-- /.col -->
            </div>
        </form>

        <!-- /.social-auth-links -->

        
        <div>
            <button id="btnsubmit" type="button" class="btn btn-primary btn-block btn-flat">登录</button>
        </div>
        <div class="text-center mt10">
            <a class="btn-glow primary login" id="btn_visitor" style="margin-right: 10px;cursor:pointer">游客</a>
            <a href="#registerDiv" data-toggle="modal" id="btn_register"  style="cursor:pointer">注册新会员&nbsp;&gt;&gt;</a>
        </div>

    </div>
    <!-- /.login-box-body -->

    <!-- /.modal -->
    <div class="modal fade" id="registerDiv" tabindex="-1" role="dialog">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <!--<h4 class="modal-title">管理员注册</h4>-->
                    <h4 class="profile-username text-center">管理员注册</h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal" role="form">
                        <div class="form-group  has-success">
                            <label class="col-md-3 control-label">姓名(*)</label>
                            <div class="col-md-8">
                                <input type="text" class="form-control" id="input_managername">
                            </div>
                        </div>
                        <div class="form-group  has-success">
                            <label class="col-md-3 control-label  has-success">管理等级(*)</label>
                            <div class="col-md-8  has-success">
                                <select id="modalselect_adminlevel">
                                    <option value="2">省级</option>
                                    <option value="3">市级</option>
                                    <option value="4">县级</option>
                                    <option value="5">乡级</option>
                                    <option value="6">村级</option>
                                </select>
                                (注：<span style="color:red">请先选择管理等级</span>）
                            </div>
                        </div>
                        <div class="form-group  has-success">
                            <label class="col-md-3 control-label  has-success">所属区域(*)</label>
                            <div class="col-md-8 has-success">
                                <select id="select_province">
                                    <option value="-1">请选择</option>
                                    <option value="150000000000">内蒙古自治区</option>
                                    <option value="510000000000">四川省</option>
                                    <option value="530000000000">云南省</option>
                                    <option value="540000000000">西藏自治区</option>
                                    <option value="610000000000">陕西省</option>
                                    <option value="620000000000">甘肃省</option>
                                    <option value="630000000000">青海省</option>
                                    <option value="640000000000">宁夏回族自治区</option>
                                    <option value="650000000000">新疆维吾尔族自治区</option>
                                    <option value="660000000">建设兵团</option>
                                </select>
                                <select id="select_city" style="display:none">
                                    <option value="-1">请选择</option>
                                </select>
                                <select id="select_county" style="display:none"><option value="-1">请选择</option></select>
                                <br /><br />
                                <select id="select_village" style="display:none"><option value="-1">请选择</option></select>
                                <select id="select_hamlet" style="display:none"><option value="-1">请选择</option></select>
                            </div>
                        </div>
                        <div class="form-group  has-success">
                            <label class="col-md-3 control-label">身份证号</label>
                            <div class="col-md-8">
                                <input type="text" class="form-control" id="input_identity">
                            </div>
                        </div>
                        <div class="form-group  has-success">
                            <label class="col-md-3 control-label">手机号码(*)</label>
                            <div class="col-md-8">
                                <input type="text" class="form-control" id="input_tel">
                            </div>
                        </div>
                        <div class="form-group  has-success">
                            <label class="col-md-3 control-label">用户名(*)</label>
                            <div class="col-md-8">
                                <input type="text" class="form-control" id="input_username">
                            </div>
                        </div>
                        <div class="form-group  has-success">
                            <label class="col-md-3 control-label">密码(*)</label>
                            <div class="col-md-8">
                                <input type="text" class="form-control" id="input_password">
                            </div>
                        </div>
                        <div class="form-group  has-success">
                            <label class="col-md-3 control-label">确认密码(*)</label>
                            <div class="col-md-8">
                                <input type="text" class="form-control" id="input_confirmpassword">
                            </div>
                        </div>
                        <div class="form-group  has-success">
                            <div class="col-md-offset-2 col-md-8">
                                <button type="button" id="btn_adduser" class="btn btn-info btn-flat">确认添加</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->

</div>
<!-- /.login-box -->

<!-- jQuery 3 -->
<script src="../bootstrap/js/jquery-latest.js"></script>
<!-- Bootstrap 3.3.7 -->
<script src="../bootstrap/js/bootstrap.js"></script>
<!-- iCheck -->
<script src="../bootstrap/js/icheck.min.js"></script>
<script>
    $(function () {
        var u_p = getCookie("aidoguser");
        if (u_p !== "" && u_p != null) {
            $("#remember_me").attr("checked", 'true');
            $("#username").val(u_p.split('+')[0]);
            $("#password").val(u_p.split('+')[1]);
        }

        $('input').iCheck({
            checkboxClass: 'icheckbox_square-blue',
            radioClass: 'iradio_square-blue',
            increaseArea: '20%' /* optional */
        });

        $("#forgetpwd").click(function () {
            alert("请联系管理员");
        });

        $("#btnsubmit").click(function () {
            var usrn = document.getElementById("username").value.trim();
            var psw = document.getElementById("password").value.trim();
            var click_type = "login";
            var userinfo = {};
            userinfo.click_type = click_type;
            userinfo.username = usrn;
            userinfo.password = psw;
            $.ajax({
                url: timestamp("/aidog/sublogin"),
                type: "POST",
                async: false,
                data: userinfo,
                success: function (data) {
                    if (data.success == false) {
                        alert("用户名或密码错误！");
                    } else {
                        if (data.data.data.managerStatus == "0") {
                            alert("账户未激活！");
                            return;
                        }
                        if ($("#remember_me")[0].checked) {
                            setCookie("aidoguser", usrn + "+" + psw, "s10000");
                        } else {
                            delCookie("aidoguser");
                        }
                        window.localStorage.setItem("aidog_token",data.data.token);
                        window.location.href = "index_iframe.html?user="+escape(usrn);
                        return true;
                    }
                }
            })
        });


        //注册相关
        var datares = {};
//        $("#btn_register").click(function () {
//            $.ajax({
//                url: "/aidog/api/register/4",
//                async:false,
//                type: "GET",
//                success: function (data) {
//                    datares = eval("(" + data + ")");
//                }
//            });
//        });
        $("#btn_register").click(function () {
            $.getJSON ("/aidog/adminlte/pages/ui_js/district.json", function (data)
            {
                datares = data;
            });
        });

        $("#btn_visitor").click(function () {
            window.location.href = timestamp("guest.html");
        });

        $('#modalselect_adminlevel').on('change', function () {
            var selectText = $(this).find('option:selected').text();
            var levelinfo = {};
            switch (selectText) {
                case "省级":
                    $("#select_city").css("display", "none");
                    $("#select_county").css("display", "none");
                    $("#select_village").css("display", "none");
                    $("#select_hamlet").css("display", "none");
                    break;
                case "市级":
                    $("#select_city").css("display", "inline");
                    $("#select_county").css("display", "none");
                    $("#select_village").css("display", "none");
                    $("#select_hamlet").css("display", "none");
                    break;
                case "县级":
                    $("#select_city").css("display", "inline");
                    $("#select_county").css("display", "inline");
                    $("#select_village").css("display", "none");
                    $("#select_hamlet").css("display", "none");
                    break;
                case "乡级":
                    $("#select_city").css("display", "inline");
                    $("#select_county").css("display", "inline");
                    $("#select_village").css("display", "inline");
                    $("#select_hamlet").css("display", "none");
                    break;
                case "村级":
                    $("#select_city").css("display", "inline");
                    $("#select_county").css("display", "inline");
                    $("#select_village").css("display", "inline");
                    $("#select_hamlet").css("display", "inline");
                    break;
            }
        });

        $("#select_province").on('change', function () {
            var selectvalue = $(this).find('option:selected').val();
            selectvalue = selectvalue.substring(0,2);
            $("#select_city").find("option").remove();
            var select_city = document.getElementById("select_city");
            select_city.options.add(new Option("请选择", "-1"));
            if ($('#modalselect_adminlevel').find('option:selected').text()!="省级") {
                for (var i = 0; i < datares.data1.length; i++) {
                    if (datares.data1[i].districtcode.toString().substring(0, 2) == selectvalue) {
                        //遍历后台传回的结果，一项项往select中添加option
                        select_city.options.add(new Option(datares.data1[i].districtname, datares.data1[i].districtcode));
                    }
                }
            }
            $("#select_county").find("option").remove();
            var select_county = document.getElementById("select_county");
            select_county.options.add(new Option("请选择", "-1"));

            $("#select_village").find("option").remove();
            var select_village = document.getElementById("select_village");
            select_village.options.add(new Option("请选择", "-1"));

            $("#select_hamlet").find("option").remove();
            var select_hamlet = document.getElementById("select_hamlet");
            select_hamlet.options.add(new Option("请选择", "-1"));
        });
        $("#select_city").on('change', function () {
            var selectvalue = $(this).find('option:selected').val();
            $("#select_county").find("option").remove();
            selectvalue = selectvalue.substring(0, 4);
            var select_county = document.getElementById("select_county");
            select_county.options.add(new Option("请选择", "-1"));
            if ($('#modalselect_adminlevel').find('option:selected').text() != "市级") {
                for (var i = 0; i < datares.data2.length; i++) {
                    if (datares.data2[i].districtcode.toString().substring(0, 4) == selectvalue) {
                        //遍历后台传回的结果，一项项往select中添加option
                        select_county.options.add(new Option(datares.data2[i].districtname, datares.data2[i].districtcode));
                    }
                }
            }
            $("#select_village").find("option").remove();
            var select_village = document.getElementById("select_village");
            select_village.options.add(new Option("请选择", "-1"));

            $("#select_hamlet").find("option").remove();
            var select_hamlet = document.getElementById("select_hamlet");
            select_hamlet.options.add(new Option("请选择", "-1"));
        });
        $("#select_county").on('change', function () {
            var selectvalue = $(this).find('option:selected').val();
            $("#select_village").find("option").remove();
            selectvalue = selectvalue.substring(0, 6);
            var select_village = document.getElementById("select_village");
            select_village.options.add(new Option("请选择", "-1"));
            if ($('#modalselect_adminlevel').find('option:selected').text() != "县级") {
                for (var i = 0; i < datares.data3.length; i++) {
                    if (datares.data3[i].districtcode.toString().substring(0, 6) == selectvalue) {
                        //遍历后台传回的结果，一项项往select中添加option
                        select_village.options.add(new Option(datares.data3[i].districtname, datares.data3[i].districtcode));
                    }
                }
            }
            $("#select_hamlet").find("option").remove();
            var select_hamlet = document.getElementById("select_hamlet");
            select_hamlet.options.add(new Option("请选择", "-1"));
        });
        $("#select_village").on('change', function () {
            var selectvalue = $(this).find('option:selected').val();
            $("#select_hamlet").find("option").remove();
            selectvalue = selectvalue.substring(0, 9);
            var select_hamlet = document.getElementById("select_hamlet");
            select_hamlet.options.add(new Option("请选择", "-1"));
            if ($('#modalselect_adminlevel').find('option:selected').text() != "乡级") {
                for (var i = 0; i < datares.data4.length; i++) {
                    if (datares.data4[i].districtcode.toString().substring(0, 9) == selectvalue) {
                        //遍历后台传回的结果，一项项往select中添加option
                        select_hamlet.options.add(new Option(datares.data4[i].districtname, datares.data4[i].districtcode));
                    }
                }
            }
        });

        $("#btn_adduser").click(function () {
            var senddata = {};
            senddata.click_type = "AddUser";
            var privilegelevel = -1;
            var adduserarea = "";
            var selectText = $("#modalselect_adminlevel").find("option:selected").text();
            switch (selectText) {
                case "省级":
                    privilegelevel = 2;
                    adduserarea = $("#select_province").find("option:selected").text();
                    break;
                case "市级":
                    privilegelevel = 3;
                    adduserarea = $("#select_province").find("option:selected").text() + "-" + $("#select_city").find("option:selected").text();
                    break;
                case "县级":
                    privilegelevel = 4;
                    adduserarea = $("#select_province").find("option:selected").text() + "-" + $("#select_city").find("option:selected").text() + "-" + $("#select_county").find("option:selected").text();
                    break;
                case "乡级":
                    privilegelevel = 5;
                    adduserarea = $("#select_province").find("option:selected").text() + "-" + $("#select_city").find("option:selected").text() + "-"
                        + $("#select_county").find("option:selected").text() + "-" + $("#select_village").find("option:selected").text();
                    break;
                case "村级":
                    privilegelevel = 6;
                    adduserarea = $("#select_province").find("option:selected").text() + "-" + $("#select_city").find("option:selected").text() + "-"
                        + $("#select_county").find("option:selected").text() + "-" + $("#select_village").find("option:selected").text() + "-" + $("#select_hamlet").find("option:selected").text();
                    break;
            }
            senddata.area = adduserarea;
            senddata.privilegelevel = privilegelevel;
            senddata.managername = $("#input_managername").val();
            senddata.address = "";
            senddata.identity = $("#input_identity").val();
            senddata.officecall = "";
            senddata.tel = $("#input_tel").val();
            senddata.username = $("#input_username").val();
            var reg = /^[a-zA-Z\d]\w{3,11}[a-zA-Z\d]$/;//正则表达式
            if (!reg.test($("#input_username").val())) {
                alert('用户名为4到12位字母(大小写敏感)，数字，下划线，不能以下划线开头和结尾！');
                return;
            }
            senddata.password = $("#input_password").val();
            senddata.addtype = "self";
            if ($("#input_managername").val() == null || $("#input_managername").val() == "" || $("#input_username").val() == null || $("#input_username").val() == "" || $("#input_password").val() == null || $("#input_password").val() == "") {
                alert("带(*)选项为必填项！");
                return;
            }
            if (adduserarea.indexOf("请选择")>0) {
                alert("地址选择有误！");
                return;
            }
            if (senddata.password != $("#input_confirmpassword").val()) {
                alert("两次输入的密码不一致！");
                //window.location.href = "#";
                return;
            } else {
                $.ajax({
                    url: "/aidog/user/register",
                    type: "POST",
                    data: senddata,
                    success: function (data) {
                        alert(data);
                        if (data == "添加用户成功！") {
                            //关闭模态框
                            alert(data);
                            $("#registerDiv").modal('hide');
//                            window.location.reload();
                        }
                    }
                })
            }
        });

    });

    function getCookie(name) {
        var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
        if (arr = document.cookie.match(reg))
            return unescape(arr[2]);
        else
            return null;
    }

    function setCookie(name, value, time) {
        var strsec = getsec(time);
        var exp = new Date();
        exp.setTime(exp.getTime() + strsec * 1);
        document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
    }

    function delCookie(name) {
        var exp = new Date();
        exp.setTime(exp.getTime() - 1);
        var cval = getCookie(name);
        if (cval != null)
            document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
    }

    function getsec(str) {
        var str1 = str.substring(1, str.length) * 1;
        var str2 = str.substring(0, 1);
        if (str2 == "s") {
            return str1 * 1000;
        }
        else if (str2 == "h") {
            return str1 * 60 * 60 * 1000;
        }
        else if (str2 == "d") {
            return str1 * 24 * 60 * 60 * 1000;
        }
    }

    function timestamp(url) {
        var getTimestamp = new Date().getTime();
        if (url.indexOf("?") > -1) {
            url = url + "&timestamp=" + getTimestamp;
        } else {
            url = url + "?timestamp=" + getTimestamp;
        }
        return url;
    }


    function keyLogin(){
        if (event.keyCode==13)  //回车键的键值为13
            document.getElementById("btnsubmit").click(); //调用登录按钮的登录事件
    }


</script>
</body>
</html>
